/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AplicacionMVC;


import java.sql.*;

/**
 *
 * @author Emilio
 */
public class BaseDatos {
    static private Connection con;
    static private Statement stmt;
    static private PreparedStatement pstmt;
    
     BaseDatos() throws SQLException{
        
    }
     
     BaseDatos(String bd) throws SQLException{
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
        con = DriverManager.getConnection(bd);
        stmt = con.createStatement();
    }
    
     void conectar() throws SQLException{
        //jdbc:oracle:thin:inftel11_11/fofito@olimpia.lcc.uma.es:1521:edgar//jdbc:oracle:thin:HR/admin@localhost:1521:XE
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        con = DriverManager.getConnection("jdbc:oracle:thin:inftel11_12/emilio@olimpia.lcc.uma.es:1521:edgar");
        stmt = con.createStatement();
     }
     
     void conectar(String bd) throws SQLException{
         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
         con = DriverManager.getConnection(bd);
         stmt = con.createStatement();
     }
     void conectar(int i, int j) throws SQLException{
         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
         con = DriverManager.getConnection("jdbc:oracle:thin:inftel11_12/emilio@olimpia.lcc.uma.es:1521:edgar");
         stmt = con.createStatement(i,j);
     }
     
    void desconectar () throws SQLException{
        con.close();
    }
    
    ResultSet consultar(String consulta) throws SQLException{
        ResultSet resultado  = stmt.executeQuery(consulta);
        return resultado;
    }
    
    int modificarBD(String consulta) throws SQLException{
        int numTuplas = stmt.executeUpdate(consulta);
        return numTuplas;
    }
    
    PreparedStatement crearPreparedStatement(String consulta) throws SQLException{
    
        pstmt=con.prepareStatement(consulta);
        return pstmt;
    }
    
    public static String getHoraActual() throws SQLException{
        String hora="";
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection c = DriverManager.getConnection("jdbc:oracle:thin:inftel11_12/emilio@olimpia.lcc.uma.es:1521:edgar");
        Statement st = c.createStatement();
        ResultSet resultado  = st.executeQuery("select to_char(sysdate,'hh24:mi')hora from dual");
        if(resultado.next())hora=resultado.getString("hora");
        c.close();
        return hora;
    }
    public static String getFechaActual() throws SQLException{
        String fecha="";
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection c = DriverManager.getConnection("jdbc:oracle:thin:inftel11_12/emilio@olimpia.lcc.uma.es:1521:edgar");
        Statement st = c.createStatement();
        ResultSet resultado  = st.executeQuery("select to_char(sysdate,'dd/mm/yyyy')fecha from dual");
        if(resultado.next())fecha=resultado.getString("fecha");
        c.close();
        return fecha;
    }
    public static String getFechaHoraActual() throws SQLException{
        String fechahora="";
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection c = DriverManager.getConnection("jdbc:oracle:thin:inftel11_12/emilio@olimpia.lcc.uma.es:1521:edgar");
        Statement st = c.createStatement();
        ResultSet resultado  = st.executeQuery("select to_char(sysdate,'dd/mm/yyyy hh24:mi')fechahora from dual");
        if(resultado.next())fechahora=resultado.getString("fechahora");
        c.close();
        return fechahora;
    }
}